//
//  QuAnalyVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/8.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "QuAnalyVC.h"

#import "FloorOneCell.h"
#import "TypeTwoCell.h"
#import "TodoHttpManager.h"

#import "SVPullToRefresh.h"
@interface QuAnalyVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSArray *titArr;
@property(nonatomic,strong)NSMutableDictionary *oneDic;
@property(nonatomic,strong)NSMutableArray *louXArr;
@property(nonatomic,strong)NSMutableArray *louYArr;
@property(nonatomic,strong)NSMutableArray *tapeXArr;
@property(nonatomic,strong)NSMutableArray *tapeYArr;
@property(nonatomic,strong)NSMutableArray *huXArr;
@property(nonatomic,strong)NSMutableArray *huYArr;
@property(nonatomic,strong)NSMutableArray *fenXArr;
@property(nonatomic,strong)NSMutableArray *fenYArr;

@property (nonatomic, strong) NSArray         *zheXianTuArr;//数据字典存放横纵坐标的数组..
@property (nonatomic, strong) NSArray         *zhuTuArr;//数据字典存放横纵坐标的数组..

@property (nonatomic, strong) NSArray         *zhemaxArr;
@property (nonatomic, strong) NSArray         *zhumaxArr;



@end

@implementation QuAnalyVC

-(void)viewWillAppear:(BOOL)animated
{
    //TODO: 去化分析
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerSecondQuHua:) name:TDManagerSecondQuHua object:nil];
    
    [_manager userManagerSecondQuHua];

}

-(void)didTDManagerSecondQuHua:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        
        _oneDic = [notifi.object objectForKey:@"data"];

        /**
         楼栋去化分析
         */
        NSMutableDictionary *louDic = [[NSMutableDictionary alloc] init];
        louDic= [_oneDic objectForKey:@"block"];
        NSMutableArray *louArr = [louDic objectForKey:@"data"];
        //NSLog(@"这个数组里有什么=====%@====%@",_oneDic,louArr);
        NSMutableArray *louDataArr = [NSMutableArray new];
        NSMutableArray *louDataNameArr = [NSMutableArray new];
        NSString *louMax = louDic[@"max_data"];
        for (int i=0; i<louArr.count; i++) {
            NSDictionary *tmpDic = louArr[i];
            [louDataArr addObject:tmpDic[@"data2"]];
            [louDataNameArr addObject:tmpDic[@"name"]];

        }
        /**
        户型去化分析
         */
        NSMutableDictionary *huDic = [[NSMutableDictionary alloc] init];
        huDic= [_oneDic objectForKey:@"room_type"];
        NSMutableArray *huArr = [huDic objectForKey:@"data"];
       // NSLog(@"这个数组里有什么=====%@====%@",_oneDic,huArr);
        NSMutableArray *huDataArr = [NSMutableArray new];
        NSMutableArray *huDataNameArr = [NSMutableArray new];
        NSString *huMax = huDic[@"max_data"];

        for (int i=0; i<huArr.count; i++) {
            NSDictionary *tmpDic = huArr[i];
            [huDataArr addObject:tmpDic[@"data2"]];
            [huDataNameArr addObject:tmpDic[@"name"]];
            
        }
        _zheXianTuArr = @[@[louDataArr,louDataNameArr],@[huDataArr,huDataNameArr],@[louMax,huMax]];
      //  _zhumaxArr = @[louMax,huMax];
        
        /**
         物业类型去化分析
         */
        NSMutableDictionary *wylxDic = [[NSMutableDictionary alloc] init];
        wylxDic= [_oneDic objectForKey:@"wylx"];
        NSMutableArray *wylxArr = [wylxDic objectForKey:@"data"];
        NSMutableArray *wylxDataArr = [NSMutableArray new];
        NSMutableArray *wylxDataNameArr = [NSMutableArray new];
        NSString *wylxMax = wylxDic[@"max_data"];

        for (int i=0; i<wylxArr.count; i++) {
            NSDictionary *tmpDic = wylxArr[i];
            [wylxDataArr addObject:tmpDic[@"data2"]];
            [wylxDataNameArr addObject:tmpDic[@"name"]];
            
        }
        /**
         分期去化分析
         */
        NSMutableDictionary *fenqiDic = [[NSMutableDictionary alloc] init];
        fenqiDic= [_oneDic objectForKey:@"qishu"];
        NSMutableArray *fenqiArr = [fenqiDic objectForKey:@"data"];
        NSMutableArray *fenqiDataArr = [NSMutableArray new];
        NSMutableArray *fenqiDataNameArr = [NSMutableArray new];
        NSString *fenqiMax = fenqiDic[@"max_data"];

        for (int i=0; i<fenqiArr.count; i++) {
            NSDictionary *tmpDic = fenqiArr[i];
            [fenqiDataArr addObject:tmpDic[@"data2"]];
            [fenqiDataNameArr addObject:tmpDic[@"name"]];
            
        }
        _zhuTuArr = @[@[wylxDataArr,wylxDataNameArr],@[fenqiDataArr,fenqiDataNameArr],@[wylxMax,fenqiMax]];
        
        
        [_tabView reloadData];
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerSecondQuHua object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BgColor;
    _zheXianTuArr = [NSArray new];
    _oneDic = [NSMutableDictionary dictionary];
    _manager = [TodoHttpManager sharedManager];
    _louXArr = [NSMutableArray array];
      _louYArr = [NSMutableArray array];
      _tapeXArr = [NSMutableArray array];
      _tapeYArr = [NSMutableArray array];
      _huXArr = [NSMutableArray array];
      _huYArr = [NSMutableArray array];
      _fenXArr = [NSMutableArray array];
      _fenYArr = [NSMutableArray array];
    _titArr = [NSArray arrayWithObjects:@"楼幢去化",@"类型去化",@"户型去化",@"分期去化", nil];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT-45-LL_TabbarHeight-LL_StatusBarAndNavigationBarHeight)];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 300;
    [self.view addSubview:_tabView];

    _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(quHuaRefresh)];
    

}

-(void)quHuaRefresh
{
     [_manager userManagerSecondQuHua];
    [self.tabView.mj_header endRefreshing];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"";
    if (indexPath.row == 0||indexPath.row == 2)
    {
        ID = @"CellA";
        FloorOneCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell)
        {
            cell = [[FloorOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.titleLab.text = [NSString stringWithFormat:@"    %@",_titArr[indexPath.row ]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (_zheXianTuArr.count!=0) {
            cell.dataArr = [NSArray arrayWithArray:_zheXianTuArr];
            cell.row = indexPath.row;

            [cell.lineChart strokePath];
        }
        return cell;
    }
    else
    {
        ID = @"CellD";
        TypeTwoCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell)
        {
            cell = [[TypeTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.typeLab.text = [NSString stringWithFormat:@"    %@",_titArr[indexPath.row ]];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        if (_zhuTuArr.count!=0) {
            cell.dataArr = [NSArray arrayWithArray:_zhuTuArr];
            cell.row = indexPath.row;
            [cell.barChart strokePath];
        }
        return cell;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
